<template>
	<view>
		<view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30" v-if="user.status===3">
			<view class="u-m-t-30 u-row-around u-flex" style="margin: 0 auto;height: 120rpx;">
				<u-modal v-model="show" show-cancel-button content="点击确定进行授权">
					<button open-type="getUserInfo" show-cancel-button class="u-reset-button" slot="confirm-button" @getuserinfo="bindGetUserInfo">确定</button>
				</u-modal>
				<u-button @click="show = true" class="u-m-r-30">用户登录</u-button>
			</view>
		</view>

		<view class="u-flex user-box u-p-l-30 u-p-r-20 u-p-b-30" v-if="user.status===1" @click="jump">
			<view class="u-m-r-10 u-m-t-40">
				<u-avatar :src="user.avatarUrl" size="140"></u-avatar>
			</view>
			<view class="u-flex-1 u-m-t-40">
				<view class="u-font-18 u-p-b-10">{{user.userName}}</view>
				<view class="u-font-14 u-tips-color">性别:{{user.gender}}</view>
			</view>
			<view class="u-m-l-10 u-p-10 u-m-t-40">
				<u-icon name="arrow-right" color="#969799" size="28"></u-icon>
			</view>
		</view>
		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item v-if="user.roleId==1||user.roleId==2||user.roleId==3" :icon-style="managericon" icon="man-add" title="管理员" @click="tomanagerpage()">
					<u-badge :count="threenews" :absolute="false" slot="right-icon"></u-badge>
				</u-cell-item>
				
				<u-cell-item icon="clock" title="学车记录" :icon-style="clockicon"  @click="jumpRecord()"></u-cell-item>

				<u-cell-item icon="file-text" title="排班表" :icon-style="Schedulicon" @click="jumpSchedule()">
					 <u-badge :count="count" :absolute="false" slot="right-icon"></u-badge>
				</u-cell-item>

				<u-cell-item icon="thumb-up" title="教练评价系统" :icon-style="Suggesticon" @click="jumpSuggest()"></u-cell-item>
				
				<u-cell-item icon="edit-pen" title="意见反馈" :icon-style="Feedbackicon" @click="jumpFeedback()"></u-cell-item>
			</u-cell-group>
		</view>
		<!-- 

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item icon="setting" title="设置"></u-cell-item>
			</u-cell-group>
		</view> -->
	</view>
</template>

<script>
	import api from "../../utils/api.js"
	export default {
		data() {
			return {
				count:0,
				threenews:0,
				show: false,
				user: {},
				managericon:{
					color:'#dfe6e9',
				},
				clockicon:{
					color:'#ffeaa7',
				},
				Schedulicon:{
					color:'#74b9ff',
				},
				Suggesticon:{
					color:'#81ecec',
				},
				Feedbackicon:{
					color:'#55efc4',
				}
				
			}
		},
		created() {
			if(uni.getStorageSync('hasappointment')>0){
				this.count = uni.getStorageSync('hasappointment')
			}
			this.getthreenews()
			this.requestdata()
			this.user = uni.getStorageSync('user')
		},
		onShow() {
			if(uni.getStorageSync('hasappointment')>0){
				this.count = uni.getStorageSync('hasappointment')
			}
			this.getthreenews()
			this.requestdata()
			this.user = uni.getStorageSync('user')
		},
		methods: {
			getthreenews(){
				api.getunreadnews(uni.getStorageSync("user").branchId).then(res=>{
					if(res.data>0){
						this.threenews = res.data
					}
				})
				
				// 通过排班 和 预约的
				let user = uni.getStorageSync("user")
				api.getUnArrageAndAppointmentList(user.branchId, 2, 2).then(res => {
					console.log("-------------------------arrange---------------------")
					console.log(res)
					this.threenews = res.data.length
					console.log(this.arrageCount)
				})
				
				api.getUnArrageAndAppointmentList(user.branchId, 2, 1).then(res => {
					console.log("----------------------appointment------------------")
					console.log(res)
					this.threenews = res.data.length
					console.log(this.appointmentCount)
				})
			},
			tomanagerpage(){
				this.threenews = 0
				wx.removeTabBarBadge({
					index:2
				})
				uni.navigateTo({
					url:'../m_user/m_user',
				})
			},
			jump: function() {
				wx.navigateTo({
					url: "/pages/uers_details/uers_details"
				})
			},
			bindGetUserInfo(e) {
				let user_info = e.mp.detail.userInfo
				let user = uni.getStorageSync('user')
				user.avatarUrl = user_info.avatarUrl
				user.userName = user_info.nickName
				user_info.gender==1?user.gender = "男":user.gender = "女"
				api.updateUserinfo(user).then((res) => {
					this.requestdata()
				})

			},
			requestdata(){
				api.getUserInfo(uni.getStorageSync('token')).then((res)=>{
						this.user = res.data.info
						uni.setStorageSync('userVO', res.data);
				})
			},
			jumpSuggest: function() {
				uni.navigateTo({
					url: '../suggest/suggest',
					success: (res) => {
						console.log("success")
					}
				})
			},
			jumpSchedule: function() {
				this.count = 0;
				uni.setStorageSync('hasappointment',0)
				wx.removeTabBarBadge({
					index:2
				})
				uni.navigateTo({
					url: '../schedule/schedule',
					success: (res) => {
						console.log("success")
					}
				})
			},
			jumpRecord: function() {
				uni.navigateTo({
					url: '../record/record',
					success: (res) => {
						console.log("success")
					}
				})
			},
			jumpFeedback: function() {
				uni.navigateTo({
					url: '../feedback/index',
					success: (res) => {
						console.log("success")
					}
				})
			}

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #ededed;
	}

	.cell-left {
		display: flex;
		align-items: center;

		.cell-icon {
			width: 40rpx;
			height: 40rpx;
		}
	}

	.camera {
		width: 54px;
		height: 44px;

		&:active {
			background-color: #ededed;
		}
	}

	.user-box {
		background-color: #fff;
	}
</style>
